package utils

import "strings"

// InputFilter 文本框过滤，space为true去空格
func InputFilter(str string, space bool) string {
	if str != "" {
		if space {
			str = strings.ReplaceAll(str, " ", "")
		}
		mapping := func(r rune) rune {
			switch {
			case r == '%':
				return '％'
			case r == '\'':
				return '‘'
			case r == '"':
				return '“'
			case r == '<':
				return '＜'
			case r == '>':
				return '＞'
			case r == '\\':
				return '、'
			case r == '\n':
				return -1
			case r == '\r':
				return -1
			case r == '#':
				return '＃'
			default:
				return r
			}
		}
		str = strings.Map(mapping, str)
	}
	return str
}

// TextFilter 文本域过滤
func TextFilter(str string) string {
	if str != "" {
		mapping := func(r rune) rune {
			switch {
			case r == '%':
				return '％'
			case r == '\'':
				return '‘'
			case r == '"':
				return '“'
			case r == '<':
				return '＜'
			case r == '>':
				return '＞'
			case r == '\\':
				return '、'
			case r == '#':
				return '＃'
			default:
				return r
			}
		}
		str = strings.Map(mapping, str)
	}
	return str
}
